package com.guigu.user.controller;


import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.user.entity.Member;
import com.guigu.user.entity.query.MemberQuery;
import com.guigu.user.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/user/member")
@Api(tags = "用户管理(pd_member表)")
@CacheConfig(cacheNames = "member")
@Transactional
public class MemberController {

    @Autowired
    private MemberService memberService;


    /**
     * TODO 需要进行短信发送
     *
     * @param member
     * @return
     */
    @ApiOperation("注册用户")
    @ApiImplicitParam(name = "member", value = "用户信息对象")
    @PostMapping("/")
    @CacheEvict(allEntries = true)
    public R save(@RequestBody Member member) {
        if (ObjectUtils.isEmpty(member)) {
            throw new TmsException(ResultCode.ERROR_CODE, "参数member为null");
        }
        member.setIdCardNoVerify(false);
        boolean b = memberService.save(member);
        return b ? new R(ResultCode.SUCCESS_CODE, "注册成功!", true) : new R(ResultCode.ERROR_CODE, "注册失败!", false);
    }


    @ApiOperation("根据id查询用户")
    @ApiImplicitParam(name = "id", value = "用户id")
    @GetMapping("/{id}")
    @Cacheable(key = "#id")
    public R queryById(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            throw new TmsException(ResultCode.ERROR_CODE, "参数id为null");
        }
        Member member = memberService.getById(id);
        return new R(ResultCode.SUCCESS_CODE, member);
    }


    @ApiOperation("根据id修改用户")
    @ApiImplicitParam(name = "Member", value = "用户信息对象")
    @PutMapping("/")
    @CacheEvict(allEntries = true)
    public R update(@RequestBody Member member) {
        if (ObjectUtils.isEmpty(member)) {
            throw new TmsException(ResultCode.ERROR_CODE, "参数member为null");
        }
        boolean b = memberService.updateById(member);
        return b ? new R(ResultCode.SUCCESS_CODE, "修改成功!", true) : new R(ResultCode.ERROR_CODE, "修改失败!", false);
    }

    @ApiOperation("根据id删除用户")
    @ApiImplicitParam(name = "id", value = "用户id")
    @DeleteMapping("/{id}")
    @CacheEvict(allEntries = true)
    public R delete(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            throw new TmsException(ResultCode.ERROR_CODE, "参数id为null");
        }
        boolean b = memberService.removeById(id);
        return b ? new R(ResultCode.SUCCESS_CODE, "删除成功!", true) : new R(ResultCode.ERROR_CODE, "删除失败!", false);
    }


    @ApiOperation(value = "查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页码"),
            @ApiImplicitParam(name = "current", value = "每页数量"),
            @ApiImplicitParam(name = "memberQuery", value = "查询对象")
    })
    @PostMapping("/queryByPhone/{pageSize}/{current}")
    public R queryByPhone(@RequestBody MemberQuery memberQuery,
                          @PathVariable String pageSize,
                          @PathVariable String current) {
        if (StringUtils.isBlank(pageSize) || !StringUtils.isNumeric(pageSize))
            pageSize = "5";
        if (StringUtils.isBlank(current) || !StringUtils.isNumeric(current))
            current = "1";
        return memberService.queryByNameOfPage(memberQuery, Integer.parseInt(pageSize), Integer.parseInt(current));
    }


    @ApiOperation("审核用户是否通过")
    @ApiImplicitParam(name = "id", value = "用户id")
    @PutMapping("/{id}")

    public R checkMemberById(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            throw new TmsException(ResultCode.ERROR_CODE, "参数id为null");
        }
        return memberService.checkMemberById(id);
    }
}

